var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');
var check = require('../../utils/check.js');
var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();


import * as echarts from '../../utils/ec-canvas/echarts.js';
function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: 130,
    height: 130
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: "#ffffff",
    color: ["#0081FF", "#B78907", "#67E0E3", "#91F2DE", "#FFDB5C", "#FF9F7F"],
    series: [{
      label: {
        normal: {
          fontSize: 9
        }
      },
      type: 'pie',
      center: ['70%', '50%'],
      radius: [0, '60%'],
      data: [{
        value: 5000,
        name: '总销量'
      }, {
        value: 500,
        name: '你的销量'
      },
      ],
      itemStyle: {
        emphasis: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 2, 2, 0.3)'
        }
      }
    }]
  };

  chart.setOption(option);
  return chart;
}
// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
    },
    shop: [],
    bb: 1,
    info: {},
    chartTitle: '总订单',
    isMainChartDisplay: true,
    ind:{}
  },

  getzt() {
    let that = this
    util.request(api.QueryAppInfo, {
    }, 'get', {
        'token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log("sadsadad" + res)
        if(res.code==200){
          that.setData({
            ind:res.data.info
          })
        }
      })
  },
  echartInit(e) {
    console.log(e)
    var timer = setTimeout(function () {
      initChart(e.detail.canvas, e.detail.width, e.detail.height);
    }, 4000);
  },
  addshop() {
    console.log('dddd');
    wx.navigateTo({
      url: '/pages/shop/shop',
    })
  },

  getshop() {
    let that = this
    util.request(api.ShowMerchantGoods, {

    }, 'POST', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          shop: res.data.goodsInfo
        })
      }
    });
  },
  up(e) {
    console.log(e)
    let that = this
    util.request(api.Operate + "/" + e.currentTarget.dataset.id + "/" + e.currentTarget.dataset.index, {

    }, 'get', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      if (res.code == 200) {
        wx.showModal({
          content: '操作成功',
          showCancel: false,
          success() {
            that.onShow()
          }
        })

      }
    });
  },

  edit() {
    this.setData({
      bb: 5
    })
  },

  ok() {
    this.setData({
      bb: 3
    })
  },
  xiugai(e) {
    // console.log(e.currentTarget.dataset.index)
    wx.navigateTo({
      url: '/pages/shop/shop?id=' + e.currentTarget.dataset.index,
    })
  },





  getorder() {

    let that = this
    util.request(api.QueryOrdersInfo, {

    }, 'GET', {
      "x-merchant-request-code": wx.getStorageSync("code"),
      'Content-Type': 'application/json;charset=UTF-8',
      'token': wx.getStorageSync('token')
    }).then(function(res) {
      console.log(res)
      that.setData({
        info: res.data
      })
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getzt() ;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getshop()
    this.getorder()
    user.checkLogin().then(res => {
    }).catch(() => {
      wx.clearStorageSync("token")
      wx.showModal({
        title: '温馨提示',
        content: '登录过期或未登录',
        showCancel: false,
        success: res => {
          wx.redirectTo({
            url: '/pages/login/login',
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})